package com.qifan.energy.mvp.presenter;

import com.qifan.energy.mvp.callback.ICallBack;
import com.qifan.energy.mvp.model.IModelImplement;
import com.qifan.energy.mvp.view.IView;

import java.util.Map;

/**
 * @author : FangShiKang
 * @date : 2020/04/11.
 * email : fangshikang@outlook.com
 * desc :  presenter层实现类,负责 V层和M层的交互,相当于他俩之间的桥梁
 */
public class IPresenterImplement implements IPresenter {

    private IView mIView;
    private IModelImplement mIModelImplement;

    public IPresenterImplement(IView IView) {
        mIView = IView;
        mIModelImplement = new IModelImplement();
    }

    /**
     * Get
     *
     * @param url
     * @param clazz
     */
    @Override
    public void onPresenterGetData(String url, Class clazz) {
        mIModelImplement.onGetData(url, clazz, new ICallBack() {
            @Override
            public void onSuccess(Object data) {
                mIView.onISuccess(data);
            }

            @Override
            public void onFailed(String error) {
                mIView.onIFailed(error);
            }
        });
    }

    /**
     * Post
     *
     * @param url
     * @param map
     * @param clazz
     */
    @Override
    public void onPresenterPost(String url, Map<String, String> map, Class clazz) {
        mIModelImplement.onPostData(url, map, clazz, new ICallBack() {
            @Override
            public void onSuccess(Object data) {
                mIView.onISuccess(data);
            }

            @Override
            public void onFailed(String error) {
                mIView.onIFailed(error);
            }
        });
    }


    /**
     * 解绑 , 防止内存泄漏
     */
    public void onDetached() {
        if (mIModelImplement != null) {
            mIModelImplement = null;
        }
        if (mIView != null) {
            mIView = null;
        }
    }

}
